package com.zhengtoon.mctoon.conv.controller.session;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.bjtoon.framework.entity.ResponseResult;
import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.controller.ConvCommentController;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.service.IOrgSessionService;
import com.zhengtoon.mctoon.conv.utils.AppConstant;
import com.zhengtoon.mctoon.conv.vo.UserTokenVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping("/session")
@Api(value = "PC端session服务", description = "PC端session服务")
public class OrgSessionController {
    private static final Logger log = LoggerFactory.getLogger(OrgSessionController.class);
    @Autowired
    private IOrgSessionService orgSessionService;

    /**
     * 获取PC端用户的sesssion
     *
     * @return ResponseResult
     * @author 144637
     */
    @ApiOperation(value = "getSessionByUserToken", httpMethod = "GET", notes = "getSessionByUserToken")
    @RequestMapping(value = "/getSessionByUserToken", method = RequestMethod.GET)
    public ResponseResult<Map<String, String>> getSessionByUserToken(
            @ApiParam(required = true, name = "userToken", value = "userToken") @RequestParam(required = true) String userToken) {
        try {
            if (StringUtils.isBlank(userToken)) {
                return new ResponseResult<>(ConvPhotoExceptionCode.USER_TOKEN_IS_NULL);
            }

            String sessionId = orgSessionService.getSessionByUserToken(userToken);

            Map<String, String> resultMap = new HashedMap();
            resultMap.put("sessionId", sessionId);
            return ResponseResult.success(resultMap);
        } catch (BusinessException e) {
            return ResponseResult.fail(AppConstant.ERROR_CODE, e.getMessage());
        } catch (Exception e) {
            log.error("获取PC端用户的sesssionId失败", e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "获取用户会话失败");
        }
    }

}
